package com.alibaba.citrus.ots.unified.inventory.repository.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;
import javax.xml.crypto.Data;

import com.alibaba.citrus.ots.common.util.JsonUtil;
import com.alibaba.citrus.ots.unified.inventory.repository.AllocateApplyOrderReadRepository;
import com.alibaba.citrus.ots.unified.inventory.utils.OtsDateUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsunifiedinventory.domain.allocateapplyorder.dto.*;
import com.epoch.app.otsunifiedinventory.domain.allocateapplyorder.model.AllocateApplyOrder;
import com.epoch.app.otsunifiedinventory.domain.allocateapplyorder.service.AllocateApplyOrderService;
import com.epoch.app.otsunifiedinventory.domain.allocateapplyorderdetail.service.AllocateApplyOrderDetailService;
import com.epoch.app.otsunifiedinventory.dto.response.AllocateApplyOrderDetailDTO;
import com.epoch.app.otsunifiedinventory.dto.response.AllocateApplyOrderListDTO;
import com.epoch.app.otsunifiedinventory.dto.response.AllocateApplyOrderResponse;
import com.epoch.app.otsunifiedinventory.model.dto.AllocateApplyOrderCodeQueryRequest;
import com.epoch.app.otsunifiedinventory.sdo.AllocateApplyOrderSDO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;


/**
 * @author heyu
 * @description
 */
@Component
public class AllocateApplyOrderReadRepositoryImpl implements AllocateApplyOrderReadRepository {

    @Resource
    private AllocateApplyOrderService allocateApplyOrderService;

    @Resource
    private AllocateApplyOrderDetailService allocateApplyOrderDetailService;

    @Override
    public List<AllocateApplyOrderListDTO> queryAllocateApplyByAllocateApplyOrderIds(List<String> allocateApplyOrderIds) {
        List<AllocateApplyOrderListDTO> allocateApplyOrderListDTOS = new ArrayList<>();
        LoadAllocateApplyOrderRequest loadAllocateApplyOrderRequest = new LoadAllocateApplyOrderRequest();
        if (CollectionUtils.isNotEmpty(allocateApplyOrderIds)){
            for (String allocateApplyOrderId:allocateApplyOrderIds){
                loadAllocateApplyOrderRequest.setId(allocateApplyOrderId);
                AllocateApplyOrder allocateApplyOrder = allocateApplyOrderService.loadAllocateApplyOrder(loadAllocateApplyOrderRequest);
                AllocateApplyOrderListDTO allocateApplyOrderListDTO = new AllocateApplyOrderListDTO();
                allocateApplyOrderListDTO.setId(allocateApplyOrder.getId());
                allocateApplyOrderListDTO.setCode(allocateApplyOrder.getCode());
                allocateApplyOrderListDTO.setType(allocateApplyOrder.getType());
                allocateApplyOrderListDTO.setApplyOrg(allocateApplyOrder.getApplyOrgId());
//                allocateApplyOrderListDTO.setReceiveWarehouse(allocateApplyOrder.getReceiveWarehouseId()!=null?allocateApplyOrder.getReceiveWarehouseId().toString():"");
                allocateApplyOrderListDTO.setCommitTime(allocateApplyOrder.getCommitTime());
//                allocateApplyOrderListDTO.setReplyTime(allocateApplyOrder.getConfirmTime());
                //TODO 提交人不确定，取的创建人
                JSONObject creator = JSON.parseObject(allocateApplyOrder.getCreator());
                if(Objects.nonNull(creator)){
                    allocateApplyOrderListDTO.setSubmitter(creator.size()==0?"":creator.getString("stageName"));
                }
//                allocateApplyOrderListDTO.setDeliverWarehouse(allocateApplyOrder.getShipWarehouseId()!=null?allocateApplyOrder.getShipWarehouseId().toString():"");
                //TODO 暂取sap预调拨单号
                allocateApplyOrderListDTO.setAllocateOrderNo(allocateApplyOrder.getOutAllocateApplyOrderId());
                allocateApplyOrderListDTO.setStatus(allocateApplyOrder.getStatus());
                allocateApplyOrderListDTOS.add(allocateApplyOrderListDTO);

            }
        }
        return allocateApplyOrderListDTOS;
    }

    @Override
    public AllocateApplyOrderResponse queryAllocateApplyOrderByCode(String code) {
        AllocateApplyOrderResponse allocateApplyOrderResponse = new AllocateApplyOrderResponse();
        LoadAllocateApplyOrderByCodeRequest loadAllocateApplyOrderByCodeRequest = new LoadAllocateApplyOrderByCodeRequest();
        loadAllocateApplyOrderByCodeRequest.setCode(code);
        AllocateApplyOrder allocateApplyOrder = allocateApplyOrderService.loadAllocateApplyOrderByCode(loadAllocateApplyOrderByCodeRequest);
        AllocateApplyOrderDetailDTO allocateApplyOrderDetailDTO = new AllocateApplyOrderDetailDTO();
        BeanUtils.copyProperties(allocateApplyOrder,allocateApplyOrderDetailDTO);
        allocateApplyOrderDetailDTO.setApplyOrg(allocateApplyOrder.getApplyOrgId());
        allocateApplyOrderDetailDTO.setType(allocateApplyOrder.getType() == null? null: allocateApplyOrder.getType().toString());
        allocateApplyOrderDetailDTO.setShipMethod(allocateApplyOrder.getShipMethod());
        allocateApplyOrderDetailDTO.setReceiveWarehouseId(Objects.isNull(allocateApplyOrder.getReceiveWarehouseId())?"":allocateApplyOrder.getReceiveWarehouseId().toString());
        allocateApplyOrderDetailDTO.setAddresscode(allocateApplyOrder.getCustomerAddress());
        allocateApplyOrderDetailDTO.setGmtCreate(allocateApplyOrder.getGmt_create());
        allocateApplyOrderDetailDTO.setShipWarehouseId(null==allocateApplyOrder.getShipWarehouseId()?"":allocateApplyOrder.getShipWarehouseId().toString());
        //单据日期
        allocateApplyOrderDetailDTO.setOrderTime(getStringDate(allocateApplyOrder.getOrderTime()));
        //生效日期
        allocateApplyOrderDetailDTO.setTakeEffectTime(getStringDate(allocateApplyOrder.getTakeEffectTime()));
        //生效至
        allocateApplyOrderDetailDTO.setInvalidDate(getStringDate(allocateApplyOrder.getInvalidDate()));
        //期望交货日期
        allocateApplyOrderDetailDTO.setHopeDeliveryTime(getStringDate(allocateApplyOrder.getHopeDeliveryTime()));
        allocateApplyOrderResponse.setAllocateApplyOrderDetailDTO(allocateApplyOrderDetailDTO);
        return allocateApplyOrderResponse;
    }

    private static String getStringDate(Date date){
        String s = "";
        if (Objects.nonNull(date)) {
            s = OtsDateUtils.formatDate(date, OtsDateUtils.DATE_FORMAT);
        }
        return s;
    }
    @Override
    public List<AllocateApplyOrderSDO> getAllocateApplyOrderSdoListByIds(List<Long> idList) {
        GetListRequest r = new GetListRequest();
        if (CollectionUtils.isNotEmpty(idList)){
            r.setIdList(idList);
        }
        r.setLimit(10000);
        r.setStart(0);
        GetListResponse response =
        allocateApplyOrderService.getList(r);

        return JsonUtil.convertList(response.getResult(), AllocateApplyOrderSDO.class);
    }

    @Override
    public AllocateApplyOrderSDO query(AllocateApplyOrderCodeQueryRequest request) {
        if (request != null && StringUtils.isNotBlank(request.getAllocateApplyOrderCode())){
            AllocateApplyOrderLoadListRequest r = new AllocateApplyOrderLoadListRequest();
            r.setCode(request.getAllocateApplyOrderCode());
            Result<List<AllocateApplyOrder>> result = allocateApplyOrderService.loadAllocateApplyOrderList(r);
            if (result != null && CollectionUtils.isNotEmpty(result.getResult())){
                return JsonUtil.convert(result.getResult().get(0),AllocateApplyOrderSDO.class);
            }
        }
        return null;
    }

    @Override
    public Result<String> getAllocateApplyOrderIdByOutAllocateApplyOrderId(String outAllocateApplyOrderId) {
        LoadAllocateApplyOrderByOutAllocateApplyOrderIdRequest request = new LoadAllocateApplyOrderByOutAllocateApplyOrderIdRequest();
        request.setOutAllocateApplyOrderId(outAllocateApplyOrderId);
        Result<List<AllocateApplyOrder>> listResult = allocateApplyOrderService.loadAllocateApplyOrderByOutAllocateApplyOrderId(request);
        Result<String> result = new Result();
        if (Objects.isNull(listResult)){
            result.setSuccess(false);
            return result;
        }
        if (CollectionUtils.isEmpty(listResult.getResult())){
            result.setSuccess(false);
            return result;
        }
        result.setResult(listResult.getResult().get(0).getCode());
        result.setSuccess(false);
        return result;
    }
}
